<template>
    <el-dialog :title="title" custom-class="el-dialog-600" v-model="visible">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
            <el-form-item label="站名" prop="title">
                <el-col :span="18">
                    <el-input
                        clearable
                        v-model="form.title"
                        placeholder="请输入网站名称"
                    ></el-input>
                </el-col>
            </el-form-item>

            <el-form-item label="邮箱" prop="email">
                <el-col :span="18">
                    <el-input
                        clearable
                        v-model="form.email"
                        placeholder="请输入站点联系邮件"
                    ></el-input>
                </el-col>
            </el-form-item>

            <el-form-item label="友链网址" prop="url">
                <el-col :span="23">
                    <el-input clearable v-model="form.url" placeholder="请输入网站地址"></el-input>
                </el-col>
            </el-form-item>

            <el-form-item label="网站图标" prop="image_url">
                <Uploader
                    v-model="form.image_url"
                    :on-success="thumbUpload"
                    :on-remove="removeThumbUpload"
                />
            </el-form-item>

            <el-form-item label="描述" prop="desc">
                <el-col :span="23">
                    <el-input
                        type="textarea"
                        :rows="2"
                        placeholder="请输入内容"
                        v-model="form.desc"
                    >
                    </el-input>
                </el-col>
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button @click="visible = false"> 取 消 </el-button>
            <el-button type="primary" :loading="loading" @click="onSubmit"> 确 认 </el-button>
        </template>
    </el-dialog>
</template>

<script>
import { getCurrentInstance, ref, reactive, watch } from 'vue';
import { adminLink as Api } from 'api';

export default {
    setup() {
        const { proxy } = getCurrentInstance();

        let id = ref(null);
        let loading = ref(false); //等待按钮
        let visible = ref(false); //弹窗开关
        let title = ref(null); // 标题
        // 表单
        let form = reactive({
            title: '',
            image_url: '',
            desc: '',
            url: '',
            email: ''
        });
        // 验证
        let rules = {
            title: [{ required: true, message: '网站名不能空', trigger: 'change' }],
            url: [
                { required: true, message: '网站地址不能空', trigger: 'change' },
                { validator: checkURL, trigger: 'change' }
            ],
            email: [
                { required: true, message: '网站邮箱不能空', trigger: 'change' },
                { validator: checkEmail, trigger: 'change' }
            ]
        };

        // 电子邮件验证
        const checkEmail = (rule, value, callback) => {
            if (proxy.$validate.isEmail(value) || value == '') {
                callback();
            } else {
                return callback(new Error('请输入正确的电子邮件'));
            }
        };
        // 域名验证
        const checkURL = (rule, value, callback) => {
            if (proxy.$validate.isURL(value) || value == '') {
                callback();
            } else {
                return callback(new Error('请输入正确的域名'));
            }
        };

        // 初始化
        const init = () => {
            title.value = id.value ? '编辑' : '新增';
            if (id.value) getById();
        };
        // 获取详情
        const getById = async () => {
            const { code, data, msg } = await proxy.$http.get(Api.getById, {
                params: { id: id.value }
            });

            if (code === 200) {
                form.title = data.title;
                form.image_url = data.image_url;
                form.desc = data.desc;
                form.url = data.url;
                form.email = data.email;
            }
        };
        // 图片上传
        const thumbUpload = (fileStr) => {
            form.image_url = fileStr;
        };
        // 删除图片
        const removeThumbUpload = (fileStr) => {
            form.image_url = fileStr;
        };
        // 提交表单
        const onSubmit = async () => {
            await proxy.$refs['formRef'].validate((valid) => {
                if (valid) loading.value = true;
            });

            if (loading.value) {
                let url = id.value ? Api.update : Api.add;
                let params = {
                    title: form.title,
                    email: form.email,
                    url: form.url,
                    image_url: form.image_url,
                    desc: form.desc
                };

                if (id.value) params.id = id.value;

                const { code, data, msg } = await proxy.$http.post(url, params);

                if (code === 200) {
                    proxy.$msg.success(msg);
                    proxy.$parent.getList();

                    visible.value = false;
                } else {
                    proxy.$msg.error(msg ? msg : data);
                }

                loading.value = false;
            }
        };

        watch(visible, (newVal, oldVal) => {
            newVal ? init() : proxy.$refs['formRef'].resetFields();
        });

        return {
            id,
            loading,
            visible,
            title,
            form,
            rules,
            thumbUpload,
            removeThumbUpload,
            onSubmit
        };
    }
};
</script>
